package com.weipai.education.service.impl;

import com.weipai.education.dao.UserCategoryDao;
import com.weipai.education.dao.UserDao;
import com.weipai.education.enums.RetCode;
import com.weipai.education.model.UserCategory;
import com.weipai.education.model.api.PageResult;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.UserCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/10 14:59
 */
@Service
public class UserCategoryServiceImpl implements UserCategoryService {
    
    @Autowired
    private UserCategoryDao userCategoryDao;
    @Autowired
    private UserDao userDao;
    
    @Override
    public Response save(UserCategory userCategory) {
        Response response = new Response();
        if(null !=userCategory.getId()){
            userCategoryDao.update(userCategory);
        }else{
            userCategory.setStatus(0);
            userCategoryDao.insert(userCategory);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response changeStatus(String id) {
        Response response = new Response();
        UserCategory userCategory = userCategoryDao.selectByPrimaryKey(id);
       if(null!=userCategory){
           if(0 ==userCategory.getStatus()){
               
               //更改停用，首先查看是否还用此类的经销商，如果有不能停用
               Integer amount = userDao.allListAmount(userCategory.getId(), null);
               if(amount>0){
                   response.setCode(RetCode.CATE_HAVE_USER_CHANGE);
                   return response;
               }else{
                   userCategory.setStatus(-1);
               }
           }else{
               userCategory.setStatus(0);
           }
           userCategoryDao.update(userCategory);
           response.setCode(RetCode.SUCCESS);
       }else{
           response.setCode(RetCode.NOT_DATA);
       }
        return response;
    }

    @Override
    public Response list(Integer page, Integer size, String dispark) {
        Response response = new Response();
        Integer start = (page-1) * size;
        List<Map<String, Object>> inventories = userCategoryDao.list(start, size, dispark);
        for(Map<String, Object> map : inventories){
            map.put("price", 0);
        }
        Integer totalNum = userCategoryDao.listAmount(dispark);
        PageResult<Map<String, Object>> pageList = new PageResult<>(inventories,page,size,totalNum);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if(userCategoryDao.batchDeleteById(id)>0){
            //经销商类别删除，对应的此类别的商品价格也进行删除--mysql外键
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }
}
